#include "Game.h"
#include "ModeCreateJoin.h"

using namespace GTL;
using namespace GTL::Strings;
using namespace GTL::Engine;

int ModeCreateJoin::Draw( float dt )
{
	Backend->ClearScreen(ColorRGBA(0,0,0,1));

	return 0;
}

int ModeCreateJoin::Load()
{
	mService = (NetworkService*)ParentGame->GetService("Network");

	GUI::EditBox* eb = (GUI::EditBox*)gMain->GetControlWithName("playername");
	eb->SetText(System::GetComputerName().c_str());

	return 0;
}

int ModeCreateJoin::Event( GameEvent event ) 
{
	if (event.EventID == GUI::Event::Click)
	{
		if (event.SenderControl()->Name == "create game")
		{
			GUI::EditBox* eb = (GUI::EditBox*)gMain->GetControlWithName("playername");
			GUI::EditBox* pb = (GUI::EditBox*)gMain->GetControlWithName("port");
			
			uint16_t port = Just::StringToInt(pb->GetText());
			ParentGame->PlayerName = eb->GetText();

			ParentGame->ServerInit(port);
			ParentGame->ClientInit("localhost", port);

			gMain->GetControlWithName("connecting")->Show();
		} 
		else if (event.SenderControl()->Name == "join game")
		{
			GUI::EditBox* eb = (GUI::EditBox*)gMain->GetControlWithName("ip");
			GUI::EditBox* pn = (GUI::EditBox*)gMain->GetControlWithName("playername");
			GUI::EditBox* pb = (GUI::EditBox*)gMain->GetControlWithName("port");
			
			uint16_t port = Just::StringToInt(pb->GetText());
			ParentGame->PlayerName = pn->GetText();

			ParentGame->ClientInit(eb->GetText().c_str(), port);

			gMain->GetControlWithName("create game")->Hide();
			gMain->GetControlWithName("join game")->Hide();

			gMain->GetControlWithName("connecting")->Show();
		} 
	} else if (event.EventID == Network::Event::Connected)
	{
		ParentGame->SetNext('lobb');
	}

	return 0;
}

int ModeCreateJoin::Unload()
{
	return 0;
}

int ModeCreateJoin::Update( float dt )
{
	return 0;
}